package com.xinghou.platform.dao.main.aspect;

import com.xinghou.platform.dao.main.common.DBUserLevelCached;
import com.xinghou.platform.model.main.UserStatistics;
import com.xinghou.platform.model.main.VersionConfig;
import com.xinghou.platform.util.JsonUtil;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * Created by qingshengzheng on 16/4/18.
 */
@Aspect
@Component
public class VersionConfigAspect extends DBUserLevelCached {
    @Pointcut("execution(public * com.xinghou.platform.dao.main.VersionConfigDAO.getVersionConfigByOsType(..))")
    public void getVersionConfigByOsType() {
    }


    @Around("getVersionConfigByOsType()")
    public VersionConfig getVersionConfigByOsType(ProceedingJoinPoint pjp) throws Throwable {
        String id = ((Object[]) pjp.getArgs())[0].toString();
        String key = String.format(VERSION_CONFIG_OSTYPE, id);
        Object jsonToken = getRedisClient().get(key, CACHED_DB_INDEX);
        if (jsonToken == null) {
            VersionConfig versionConfig = (VersionConfig) pjp.proceed();
            if (versionConfig != null)
                getRedisClient().set(key, JsonUtil.toJson(versionConfig), CACHED_DB_INDEX);
            return versionConfig;
        } else {
            VersionConfig versionConfig = (VersionConfig) JsonUtil.fromJson(jsonToken.toString(), VersionConfig.class);
            return versionConfig;
        }
    }

}
